package com.appleframework.module.usermgr.dao;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.appleframework.core.BaseDao;
import com.appleframework.module.usermgr.vo.Diningtable;
import com.appleframework.module.usermgr.vo.Item;
import com.appleframework.module.usermgr.vo.Itemdetail;

@Repository("diningtableMgrDao")
public class DiningtableMgrDao extends BaseDao{
	
	/**
	 * 添加  
	 * @param b 要保存的记录
	 * 
	 */
	public void add(Diningtable d){
		String sql="insert into t_diningtable (name,descript,fit,status,is_delete,createtime) "+
		"values ('"+d.getName()+"','"+d.getDescript()+"',"+d.getFit()+",0,0,now())";
	    System.out.println(sql);
		getJdbcTemplate().update(sql);
		
	}
	
	
	/**
	 * 根据id'删除记录  
	 * @param id要删除记录的id
	 * 
	 */
	public void delete(Integer id){
		String sql="update t_diningtable set is_delete=1 where id="+id;
		System.out.println(sql);
		getJdbcTemplate().update(sql);
		
	};
	
	
	
	/**
	 * 修改信息  
	 * @param b 要修改的记录
	 * 
	 */
	public void update(Diningtable d){
		String sql="update t_diningtable set name='"+d.getName()
        +"',descript='"+d.getDescript()+"',fit="+d.getFit()
        +",status="+d.getStatus()+",updatetime=now() where id="+d.getId();
		System.out.println(sql);
		getJdbcTemplate().update(sql);
		
	};
	
	
	

	/**
	 * 根据Id查询一条 记录
	 * @param id查询出来的一条记录
	 * 
	 */
	public Diningtable findById(Integer id){
		String sql="select * from t_diningtable where id="+id;
		System.out.println(sql);
		return	(Diningtable) getJdbcTemplate().query(sql, BeanPropertyRowMapper.newInstance(Diningtable.class)).get(0);
		
	};
	
	
	/**
	 * 分页显示信息  --根据name/status/fit/所有，进行分页显示未删除餐桌
	 * @param start记录开始
	 * @param pageSize每一页显示的条数
	 * @param name 餐桌名称
	 * @param status 餐桌状态0空/1满
	 * @param fit座位数
	 * @return
	 */
	public List<Diningtable> getPage(Integer start,Integer pageSize,String name,Integer status,Integer fit){
		
		//String sql="select * from t_item ";
		StringBuffer sb = new StringBuffer();
		sb.append("select * from t_diningtable where is_delete=0 ");
		
			if(null != name && !"".equals(name)){	
				sb.append("and  name like "); 
				sb.append("'%");
				sb.append(name);
				sb.append("%' ");
			}
			if (null != status) {
				sb.append(" and status=");
				sb.append(status);
			}
			if (null != fit) {
				sb.append(" and fit=");
				sb.append(fit);
			}
		
		sb.append(" limit ");
		sb.append(start);
		sb.append(",");
		sb.append(pageSize);
		
		System.out.println(sb.toString());
		return getJdbcTemplate().query(sb.toString(),BeanPropertyRowMapper.newInstance(Diningtable.class) );
		
		
	};
	
	

	/**
	 * 显示所有记录 ----未删除的桌子
	 * 
	 * @return 所有记录
	 */
	public List<Diningtable> findAll(){
		String sql="select * from t_diningtable where is_delete=0";
		return getJdbcTemplate().query(sql, BeanPropertyRowMapper.newInstance(Diningtable.class));
	};
	
	
	
	/**
	 * 查询一共多少条记录  
	 * @param name 名称
	 * @param status 餐桌状态0空/1满
	 * @param fit 座位数
	 * @return
	 */
	public int findRecordCount(String name,Integer status,Integer fit){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(*) from t_diningtable where is_delete=0 ");
		if(null != name && !"".equals(name)){	
			sb.append("and  name like "); 
			sb.append("'%");
			sb.append(name);
			sb.append("%' ");
		}
		if (null != status) {
			sb.append(" and status=");
			sb.append(status);
		}
		if (null != fit) {
			sb.append(" and fit=");
			sb.append(fit);
		}
		
		
		return getJdbcTemplate().queryForInt(sb.toString());
	};
	
	

}
